﻿/*
	Copyright (c) 2013 Andrew Meyer

	Permission is hereby granted, free of charge, to any person obtaining a copy of this software
	and associated documentation files (the "Software"), to deal in the Software without
	restriction, including without limitation the right to use, copy, modify, merge, publish,
	distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
	Software is furnished to do so, subject to the following conditions:

	The above copyright notice and this permission notice shall be included in all copies or
	substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
	BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
	NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
	DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

using System.Windows;
using System.Windows.Media.Media3D;

namespace Ascend
{
	/// <summary>
	/// Class for representing a camera and a scene graph, viewable via Ascend.SceneViewer3D.
	/// This class cannot be inherited.
	/// </summary>
	public sealed class Scene3D : DependencyObject
	{
		#region Lifecycle
		/// <summary>
		/// Initializes a new instance of the Ascend.Scend3D class.
		/// </summary>
		public Scene3D()
		{
			// Set default render root scene node
			RenderRoot = new ModelSceneNode();
			RenderRoot.Name = "__root__";

			// Set default camera
			Camera = new PerspectiveCamera(new Point3D(0, 0, 1), new Vector3D(0, 0, -1),
				new Vector3D(0, 1, 0), 70.0);
		}
		#endregion

		#region Dependency Properties
		/// <summary>
		/// Gets the root Ascend.SceneNode of this Ascend.Scene3D's scene graph.
		/// </summary>
		public ModelSceneNode RenderRoot
		{
			get
			{
				return (ModelSceneNode)GetValue(RenderRootProperty);
			}
			
			private set
			{
				SetValue(RenderRootProperty, value);
			}
		}

		/// <summary>
		/// Identifies the Ascend.Scene3D.RenderRoot dependency property.
		/// </summary>
		public static readonly DependencyProperty RenderRootProperty =
			DependencyProperty.Register("RenderRoot", typeof(ModelSceneNode), typeof(Scene3D));


		/// <summary>
		/// Gets or sets the primary camera for this Ascend.Scene3D.
		/// </summary>
		public ProjectionCamera Camera
		{
			get
			{
				return (ProjectionCamera)GetValue(CameraProperty);
			}

			set
			{
				SetValue(CameraProperty, value);
			}
		}

		/// <summary>
		/// Identifies the Ascend.Scene3D.Camera dependency property.
		/// </summary>
		public static readonly DependencyProperty CameraProperty =
			DependencyProperty.Register("Camera", typeof(ProjectionCamera), typeof(Scene3D));
		#endregion

		#region Public Methods
		/// <summary>
		/// Points the camera to the specified point.
		/// </summary>
		/// <param name="point">
		/// The point at which to look.
		/// </param>
		public void LookAt(Point3D point)
		{
			if (Camera != null)
			{
				Camera.LookDirection = point - Camera.Position;

				// Keep from rolling the camera
				Camera.UpDirection = new Vector3D(0, 1, 0);
			}
		}

		/// <summary>
		/// Points the camera to the specified Ascend.TransformableObject.
		/// </summary>
		/// <param name="target">
		/// The Ascend.TransformableObject at which to look.
		/// </param>
		public void LookAt(TransformableObject target)
		{
			LookAt(target.GlobalTransform.Transform(new Point3D(0, 0, 0)));
		}
		#endregion
	}
}
